<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="交易流水号" v-bind="validateInfos.refundFlowNo">
            <a-input v-model:value="formData.refundFlowNo" placeholder="请输入交易流水号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="平台退款流水号" v-bind="validateInfos.refundPlatformNo">
            <a-input v-model:value="formData.refundPlatformNo" placeholder="请输入平台退款流水号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="银行退款流水号" v-bind="validateInfos.refundBankNo">
            <a-input v-model:value="formData.refundBankNo" placeholder="请输入银行退款流水号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="订单号" v-bind="validateInfos.orderNo">
            <a-input v-model:value="formData.orderNo" placeholder="请输入订单号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保单号" v-bind="validateInfos.policyNo">
            <a-input v-model:value="formData.policyNo" placeholder="请输入保单号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="区域" v-bind="validateInfos.areaId">
            <j-dict-select-tag v-model:value="formData.areaId" dictCode="JYPTBM" placeholder="请选择退款银行类型" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="订单来源" v-bind="validateInfos.orderSource">
	          <j-dict-select-tag v-model:value="formData.orderSource" dictCode="PPYWLX" placeholder="请选择订单来源" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="退款状态" v-bind="validateInfos.refundState">
            <j-dict-select-tag v-model:value="formData.refundState" dictCode="TKZT" placeholder="请选择退款状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="保险公司" v-bind="validateInfos.insuranceCompany">
            <a-input v-model:value="formData.insuranceCompany" placeholder="请输入保险公司" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="出保时间" v-bind="validateInfos.insureActiveDate">
		        <a-date-picker placeholder="请选择出保时间" v-model:value="formData.insureActiveDate" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="退保时间" v-bind="validateInfos.surrenderTime">
		        <a-date-picker placeholder="请选择退保时间" v-model:value="formData.surrenderTime" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="退款时间" v-bind="validateInfos.refundDate">
		        <a-date-picker placeholder="请选择退款时间" v-model:value="formData.refundDate" value-format="YYYY-MM-DD"  style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="付款人账号" v-bind="validateInfos.payAccount">
            <a-input v-model:value="formData.payAccount" placeholder="请输入付款人账号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="付款人名称" v-bind="validateInfos.payAccountName">
            <a-input v-model:value="formData.payAccountName" placeholder="请输入付款人名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="付款人银行名称" v-bind="validateInfos.partyBankName">
            <a-input v-model:value="formData.partyBankName" placeholder="请输入付款人银行名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="退款金额" v-bind="validateInfos.amount">
	          <a-input-number v-model:value="formData.amount" placeholder="请输入退款金额" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="退款手续费" v-bind="validateInfos.tranFee">
	          <a-input-number v-model:value="formData.tranFee" placeholder="请输入退款手续费" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="退款银行类型" v-bind="validateInfos.refundBankType">
            <a-input v-model:value="formData.refundBankType" placeholder="请输入退款银行类型" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="退款联行号" v-bind="validateInfos.unionBankNo">
            <a-input v-model:value="formData.unionBankNo" placeholder="请输入退款联行号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="退款联行名称" v-bind="validateInfos.unionBankName">
            <a-input v-model:value="formData.unionBankName" placeholder="请输入退款联行名称" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSelectDept from '/@/components/Form/src/jeecg/components/JSelectDept.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../InsuranceRefund.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    refundFlowNo: '',   
    refundPlatformNo: '',   
    refundBankNo: '',   
    orderNo: '',   
    policyNo: '',   
    areaId: '',   
    orderSource: '',   
    refundState: '',   
    insuranceCompany: '',   
    insureActiveDate: '',   
    surrenderTime: '',   
    refundDate: '',   
    payAccount: '',   
    payAccountName: '',   
    partyBankName: '',   
    amount: undefined,
    tranFee: undefined,
    refundBankType: '',   
    unionBankNo: '',   
    unionBankName: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    orderNo: [{ required: true, message: '请输入订单号!'},],
    policyNo: [{ required: true, message: '请输入保单号!'},],
    areaId: [{ required: true, message: '请输入区域!'},],
    orderSource: [{ required: true, message: '请输入订单来源!'},],
    refundState: [{ required: true, message: '请输入退款状态!'},],
    insuranceCompany: [{ required: true, message: '请输入保险公司!'},],
    surrenderTime: [{ required: true, message: '请输入退保时间!'},],
    refundDate: [{ required: true, message: '请输入退款时间!'},],
    payAccount: [{ required: true, message: '请输入付款人账号!'},],
    payAccountName: [{ required: true, message: '请输入付款人名称!'},],
    amount: [{ required: true, message: '请输入退款金额!'},],
    refundBankType: [{ required: true, message: '请输入退款银行类型!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
